/*
 * @Description: 用数组实现栈
 * @Version: 1.0
 * @Author: biao.hu
 * @Date: 2021-10-26 21:58:00
 * @LastEditors: biao.hu
 * @LastEditTime: 2021-10-29 15:08:30
 */
package Queue

import "fmt"

type (
	ArrayStack struct {
		Data      []int
		Length    int
		MaxLength int
	}
)

/**
 * @description: New一个ArrayStack
 * @param {int} maxLength
 * @return {* ArrayStack}
 */
func NewArrayStack(maxLength int) *ArrayStack {
	return &ArrayStack{Data: []int{}, Length: 0, MaxLength: maxLength}
}

/**
 * @description: push 数据进栈
 * @param {int} val
 * @return {error}
 */
func (st *ArrayStack) Push(val int) error {
	if st.MaxLength == st.Length {
		fmt.Println("The maximum length has been reached, maxLength:", st.MaxLength)
		return nil
	}
	st.Data = append(st.Data, val)
	st.Length++
	return nil
}

/**
 * @description: pop数据出栈
 * @param {*}
 * @return {int}
 */
func (st *ArrayStack) Pop() int {
	if st.Length < 1 {
		panic("st.Length is 0, nodata")
	}
	val := st.Data[st.Length-1]
	st.Data = st.Data[:st.Length-1]
	st.Length--
	fmt.Print(val, " ")
	return val
}

/**
 * @description: 把该方法放在main里面执行即可
 * @param {*}
 * @return {*}
 */
func RunArrayStack() {
	count := 10
	st := NewArrayStack(count)
	for i := 0; i < count; i++ {
		st.Push(i)
	}
	fmt.Println(st.Data)
	for i := 0; i < count; i++ {
		st.Pop()
	}
	fmt.Println()
	fmt.Println(st.Data)
}
